package org.xiangxji.snippets.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import org.xiangxji.snippets.db.DBConnectUtils;
import org.xiangxji.snippets.file.FileUtil;


public class ImportTask extends TimerTask {

	private int dth;
	
	private int tth;

	public ImportTask() {
		
	}
	public ImportTask(int dth,int t) {
		this.setDth(dth);
		this.setTth(t);
	}
	
	@Override
	public void run() {
		try {
			importToDB();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void writeLog(String s) {
		String filename = "D:\\\\backup\\\\import.log";
		RandomAccessFile rf;
		try {
			rf = new RandomAccessFile(filename,"rw");//定义一个类RandomAccessFile的对象，并实例化
			rf.seek(rf.length());//将指针移动到文件末尾
			rf.writeBytes(s+"\n");
			rf.close();//关闭文件流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@SuppressWarnings("deprecation")
	public void importToDB() throws IOException {
		Date createDay = new Date(110, 11, dth);
		
		Connection conn = DBConnectUtils.getConnection("jdbc:mysql://localhost:3306/trade_download_log", "root", "");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		String t = sdf.format(createDay);
		Statement stmt;
		PreparedStatement pstmt;
//		int d = createDay.getDay();
		try {
			stmt = conn.createStatement();
			String sql = "delete from trade_download_report_"+tth;
			stmt.execute(sql);
		} catch (SQLException e1) {
			writeLog("error delete ");
			System.out.println("error");
			return;
		}
		
		String[] resources = FileUtil.splitDataFile("d:\\\\backup\\\\201012"+dth+"\\\\tradeDownloadLogFinal.log", 100);
		for(int i=0;i<resources.length;i++) {
			System.out.println(tth+"-"+resources[i]);
			File file = new File(resources[i]);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String temp=null;
			int j=0;
			try {
//				stmt = conn.createStatement();
				pstmt = conn.prepareStatement("insert into trade_download_report_"+tth+
						" (api,appkey, seller_nick, seller_type,tid, type, payment, total_fee, tradefrom, status, created, dump_time)"+
						"values (?,?,?,?,?,?,?,?,?,?,?,?)");
				while((temp=br.readLine())!=null) {
					String[] arr = temp.split(",");
					if(arr.length<11) {
						writeLog(temp);
						System.out.println("1");
					}else {
						Date createDate = new Date(Long.valueOf(arr[10]));
						String c = sdf.format(createDate);
						pstmt.setString(1, arr[0]);
						pstmt.setString(2, arr[1]);
						pstmt.setString(3, arr[2]);
						pstmt.setString(4, arr[3]);
						pstmt.setLong(5, Long.parseLong(arr[4]));
						pstmt.setInt(6, Integer.parseInt(arr[5]));
						pstmt.setDouble(7, Double.parseDouble(arr[6]));
						pstmt.setDouble(8, Double.parseDouble(arr[7]));
						pstmt.setString(9, arr[8]);
						pstmt.setInt(10, Integer.parseInt(arr[9]));
						pstmt.setString(11, c);
						pstmt.setString(12, t);

						pstmt.addBatch();
						j++;
						if(j%1000==0 && j!=0) {
							pstmt.executeBatch();
							pstmt.clearBatch();
							j=0;
						}
					}
				}
				if(j!=0) {
					pstmt.executeBatch();
					pstmt.clearBatch();
					j=0;
				}
				pstmt.close();
			} catch (SQLException e) {
				writeLog(temp);
				System.out.println(e.getMessage()); 
				System.out.println("3");
			}
			br.close();
			fr.close();
			try {
				Thread.sleep(800);
			} catch (InterruptedException e) {
				writeLog(resources[i]);
				continue;
			}
		}
	}	
	
	public void setDth(int dth) {
		this.dth = dth;
	}
	public int getDth() {
		return dth;
	}
	public void setTth(int tth) {
		this.tth = tth;
	}
	public int getTth() {
		return tth;
	}

}
